/**
 * 将 body 参数序列化为字符串
 * */
export const stringifyBody = (body: string | Record<string, any>): string => {
  return typeof body === 'string' ? body : JSON.stringify(body)
}

/**
 * 获取新的 params 或 data 参数
 *
 * @param appid
 * @param functionId
 * @param payload 参数
 * @param reinforce 是否加固
 * */
export const getNewPayload = async (
  appid: string,
  functionId: string,
  payload?: Record<string, any>,
  reinforce?: boolean,
): Promise<Record<string, any>> => {
  let newPayload = payload || {}
  const bodyStr = newPayload.body ? stringifyBody(newPayload.body) : ''
  // 通用参数
  const commonParams = {
    appid,
    functionId,
    client: newPayload.client || 'pc',
    clientVersion: newPayload.clientVersion || '1.0.0',
  }

  if (reinforce) {
    // 加固后组装参数
    newPayload = {}
  } else {
    // 不加固
    newPayload = commonParams
  }

  if (bodyStr) {
    newPayload.body = bodyStr
  }

  return newPayload
}
